package com.py.credit.controller;

import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.Statistics;
import com.py.credit.entity.StatisticsCount;
import com.py.credit.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/statistics")
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;

    /**
     * 联合奖惩发起数
     *
     * @param type      主体类型：C：法人 P:自然人
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping("/selectInfo.json")
    public RestServiceResult selectOneByAjbh(@RequestParam(defaultValue = "", required = true, value = "type") String type,
                                             @RequestParam(defaultValue = "", required = true, value = "startTime") String startTime,
                                             @RequestParam(defaultValue = "", required = true, value = "endTime") String endTime) throws ParseException {
        Date dNow = new Date();   //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(calendar.MONTH, -11);  //设置为前12月
        dBefore = calendar.getTime();   //得到前12月的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM"); //设置时间格式
        String defaultStartDate = sdf.format(dBefore);    //格式化前12月的时间
        String defaultEndDate = sdf.format(dNow); //格式化当前时间
        Map<String, Object> map = new HashMap<String, Object>();
        //联合奖惩类型，J表示激励，c表示惩戒
        if (type.equals("C")) {
            List<Statistics> cjTotal = statisticsService.selectCjInfo(type, startTime, endTime);
            List<Statistics> jlTotal = statisticsService.selectJlInfo(type, startTime, endTime);
            map.put("cjTotal", cjTotal);
            map.put("jlTotal", jlTotal);
            if (startTime.equals("") || startTime == null) {
                startTime = defaultStartDate;
            } else {
                SimpleDateFormat sdfnew = new SimpleDateFormat("yyyy-MM-dd");
                startTime = sdf.format(sdfnew.parse(startTime));
            }

            if (endTime.equals("") || endTime == null) {
                endTime = defaultEndDate;
            } else {
                SimpleDateFormat sdfnew = new SimpleDateFormat("yyyy-MM-dd");
                endTime = sdf.format(sdfnew.parse(endTime));
                ;
            }
            map.put("startTime", startTime);
            map.put("endTime", endTime);
        } else if (type.equals("P")) {
            List<Statistics> cjTotal = statisticsService.selectCjInfo(type, startTime, endTime);
            List<Statistics> jlTotal = statisticsService.selectJlInfo(type, startTime, endTime);
            map.put("cjTotal", cjTotal);
            map.put("jlTotal", jlTotal);
            if (startTime.equals("") || startTime == null) {
                startTime = defaultStartDate;
            } else {
                SimpleDateFormat sdfnew = new SimpleDateFormat("yyyy-MM-dd");
                startTime = sdf.format(sdfnew.parse(startTime));
            }

            if (endTime.equals("") || endTime == null) {
                endTime = defaultEndDate;
            } else {
                SimpleDateFormat sdfnew = new SimpleDateFormat("yyyy-MM-dd");
                endTime = sdf.format(sdfnew.parse(endTime));
                ;
            }
            map.put("startTime", startTime);
            map.put("endTime", endTime);
        }

        return new RestServiceResult(map);
    }

    /**
     * 6个月联合奖惩发起数量走势图
     *
     * @param type      主体类型：C：法人 P:自然人
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping("/selectSixMorth.json")
    public RestServiceResult selectNum(@RequestParam(defaultValue = "", required = true, value = "type") String type,
                                       @RequestParam(defaultValue = "", required = true, value = "startTime") String startTime,
                                       @RequestParam(defaultValue = "", required = true, value = "endTime") String endTime) throws ParseException {
        Date dNow = new Date();   //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(calendar.MONTH, -5);  //设置为前6月
        dBefore = calendar.getTime();   //得到前6月的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM"); //设置时间格式
        String defaultStartDate = sdf.format(dBefore);    //格式化前6月的时间
        String defaultEndDate = sdf.format(dNow); //格式化当前时间
        Map<String, Object> map = new HashMap<String, Object>();
        //联合奖惩类型，J表示激励，c表示惩戒
        if (type.equals("C")) {
            List<Statistics> cjTotal = statisticsService.selectSixMorthCjInfo(type, startTime, endTime);
            List<Statistics> jlTotal = statisticsService.selectSixMorthJlInfo(type, startTime, endTime);
            if (startTime.equals("") || startTime == null) {
                startTime = defaultStartDate;
            } else {
                SimpleDateFormat sdfnew = new SimpleDateFormat("yyyy-MM-dd");
                startTime = sdf.format(sdfnew.parse(startTime));
            }

            if (endTime.equals("") || endTime == null) {
                endTime = defaultEndDate;
            } else {
                SimpleDateFormat sdfnew = new SimpleDateFormat("yyyy-MM-dd");
                endTime = sdf.format(sdfnew.parse(endTime));
                ;
            }
            map.put("cjTotal", cjTotal);
            map.put("jlTotal", jlTotal);
            map.put("startTime", startTime);
            map.put("endTime", endTime);
        } else if (type.equals("P")) {
            List<Statistics> cjTotal = statisticsService.selectSixMorthCjInfo(type, startTime, endTime);
            List<Statistics> jlTotal = statisticsService.selectSixMorthJlInfo(type, startTime, endTime);
            if (startTime.equals("") || startTime == null) {
                startTime = defaultStartDate;
            } else {
                SimpleDateFormat sdfnew = new SimpleDateFormat("yyyy-MM-dd");
                startTime = sdf.format(sdfnew.parse(startTime));
            }

            if (endTime.equals("") || endTime == null) {
                endTime = defaultEndDate;
            } else {
                SimpleDateFormat sdfnew = new SimpleDateFormat("yyyy-MM-dd");
                endTime = sdf.format(sdfnew.parse(endTime));
                ;
            }
            map.put("cjTotal", cjTotal);
            map.put("jlTotal", jlTotal);
            map.put("startTime", startTime);
            map.put("endTime", endTime);
        }

        return new RestServiceResult(map);
    }

    /**
     * 各部门联合奖惩反馈情况top10
     *
     * @param type      主体类型：C：法人 P:自然人
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectDeptResponse.json")
    public RestServiceResult selectDeptResponse(@RequestParam(defaultValue = "", required = true, value = "type") String type,
                                                @RequestParam(defaultValue = "", required = true, value = "startTime") String startTime,
                                                @RequestParam(defaultValue = "", required = true, value = "endTime") String endTime) {
        Map<String, Object> map = new HashMap<String, Object>();
        //联合奖惩类型，J表示激励，c表示惩戒
        if (type.equals("C")) {
            List<Statistics> deptYg = statisticsService.selectDeptYgInfo(type, startTime, endTime);
            List<Statistics> DeptSj = statisticsService.selectDeptSjInfo(type, startTime, endTime,deptYg);
            map.put("deptYg", deptYg);
            map.put("DeptSj", DeptSj);
        } else if (type.equals("P")) {
            List<Statistics> deptYg = statisticsService.selectDeptYgInfo(type, startTime, endTime);
            List<Statistics> DeptSj = statisticsService.selectDeptSjInfo(type, startTime, endTime,deptYg);
            map.put("deptYg", deptYg);
            map.put("DeptSj", DeptSj);
        }
        return new RestServiceResult(map);
    }

    /**
     * 联合惩戒推动效果图
     *
     * @param type      主体类型：C：法人 P:自然人
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectCjEffect.json")
    public RestServiceResult selectCjEffect(@RequestParam(defaultValue = "", required = true, value = "type") String type,
                                            @RequestParam(defaultValue = "", required = true, value = "startTime") String startTime,
                                            @RequestParam(defaultValue = "", required = true, value = "endTime") String endTime) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (type.equals("C")) {
            int frZs = statisticsService.selectFrCjZs(type, startTime, endTime);
            List<Statistics> total = statisticsService.selectFrCjYC(type, startTime, endTime);
            map.put("zs", frZs);
            map.put("total", total);
        } else if (type.equals("P")) {
            int frZs = statisticsService.selectZrrCjZs(type, startTime, endTime);
            List<Statistics> total = statisticsService.selectZrrCjYC(type, startTime, endTime);
            map.put("zs", frZs);
            map.put("total", total);
        }

        return new RestServiceResult(map);
    }

    /**
     * Excel统计导出
     *
     * @param response
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @throws IOException
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping(value = "/exportExcel.json")
    public void ExcelExport(HttpServletResponse response,
                            @RequestParam(value = "startTime", defaultValue = "", required = true) String startTime,
                            @RequestParam(value = "endTime", defaultValue = "", required = true) String endTime
    ) throws IOException, ParseException {
        statisticsService.excelExport(response, startTime, endTime);
    }

    /**
     * 联合奖惩事件各规则引用的占比
     *
     * @param type      主体类型：C：法人 P:自然人
     * @param jclb      奖惩类别：C:惩戒 J:激励
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @ResponseBody
    @RequestMapping("/jcgzDistribution.json")
    public RestServiceResult selectJcgzDistribution(@RequestParam(defaultValue = "", required = true, value = "type") String type,
                                                    @RequestParam(defaultValue = "", required = true, value = "jclb") String jclb,
                                                    @RequestParam(defaultValue = "", required = true, value = "startTime") String startTime,
                                                    @RequestParam(defaultValue = "", required = true, value = "endTime") String endTime) {
        List<StatisticsCount> jcgzDistribution = statisticsService.selectJcgzDistribution(type, jclb, startTime, endTime);
        return new RestServiceResult(jcgzDistribution);
    }

    /**
     * 信用主体联合奖惩事件次数排名统计top10
     *
     * @param type      主体类型：C：法人 P:自然人
     * @param jclb      奖惩类别：C:惩戒 J:激励
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @ResponseBody
    @RequestMapping("/jcCount.json")
    public RestServiceResult selectJcCount(@RequestParam(defaultValue = "", required = true, value = "type") String type,
                                           @RequestParam(defaultValue = "", required = true, value = "jclb") String jclb,
                                           @RequestParam(defaultValue = "", required = true, value = "startTime") String startTime,
                                           @RequestParam(defaultValue = "", required = true, value = "endTime") String endTime) {
        List<StatisticsCount> jcCount = statisticsService.selectJcCount(type, jclb, startTime, endTime);
        return new RestServiceResult(jcCount);
    }
}
